package be.ac.ua.comp.projectGPT.auctioneers;

//~--- non-JDK imports --------------------------------------------------------

import be.ac.ua.comp.projectGPT.auctions.Auction;
import be.ac.ua.comp.projectGPT.biddingAgents.BiddingAgent;
import be.ac.ua.comp.projectGPT.biddingAgents.EnglishBiddingAgent;
import be.ac.ua.comp.projectGPT.tuple.Tuple;
import be.ac.ua.comp.projectGPT.types.English;

//~--- JDK imports ------------------------------------------------------------

import java.util.Vector;

public class EnglishAuctioneer extends Auctioneer implements English {


    public EnglishAuctioneer(String name) {
        this(name, null);
    }

    public EnglishAuctioneer(String name, Auction<English> auction) {
        super(name, auction);
    }


    @Override
    public void runBiddingRound() {
        logger.info("Running bidding round "+auction.getBiddingRound());
        if(auction.getBiddingRound() == 1) {
                    for(BiddingAgent agent : (Vector<EnglishBiddingAgent>) biddingAgents) {
            agent.start();
        }
            highestBidder = new Tuple<BiddingAgent,Long>(null,auction.getStartingBid());
        }
        for(BiddingAgent agent : (Vector<EnglishBiddingAgent>) biddingAgents) {
            logger.info("Asking bid from "+agent.getName());
            long agentBid = agent.askBid((Long)highestBidder.getSnd());
            if(!(agentBid == 0)) {
                auction.newBidding();
            }
            logger.info("Checking acceptability of bid "+agentBid);
            if(biddingPolicy.accept(agentBid,(Long)highestBidder.getSnd())) {
                logger.info("Bid accepted");
                if(agentBid > (Long) highestBidder.getSnd()) {
                    logger.info("Highest bid, new highest bidder: "+agent.getName()+" with "+agentBid);
                    highestBidder = new Tuple<BiddingAgent,Long>(agent,agentBid);
                } else {
                    logger.info("Not the highest bid, current highest bid: "+highestBidder.getSnd());
                }
            } else {
                logger.info("Bid rejected");
            }
        }
    }
}
